package com.bto.solar.work.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

/**
 * @author 王小波
 * @description:
 * @date 2023/8/29 10:51
 */
public class BeanUtils {
    public static void copyPropertiesIgnoreNullValue(Object source , Object target){
        BeanUtil.copyProperties(source , target , CopyOptions.create().setIgnoreNullValue(true));
    }

    public static void copyPropertiesConvertJson(Object source , Object target) {
        if(NullUtils.isAnyNull(source , target)){
            return;
        }
        BeanUtil.copyProperties(source, target , CopyOptions.create().setIgnoreNullValue(true).setConverter((fieldType , fieldValue) ->{
            if (null == fieldValue) {
                return null;
            }
            if (fieldType.getTypeName().contains("JSONObject")) {
                fieldValue = JSONObject.parseObject(fieldValue.toString());
            }
            else if (fieldType.getTypeName().contains("JSONArray")){
                fieldValue = JSONArray.parse(fieldValue.toString());
            }
            return fieldValue;
        }));
    }

}
